package com.ji.controller;

import com.ji.dao.UserMapper;
import com.ji.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/users")
    public String listUser(Model model){

        List<User> users = userMapper.queryAllUser ();
        model.addAttribute ("userList",users);
        return "user/userList";
    }
    @GetMapping("/toadduser")
    public String toaddUser(){

        return "user/addUser";
    }
    @PostMapping("/toadduser")
    public String addUser(User user){

        int i = userMapper.addUser (user);
        if(i>0) System.out.println ("添加成功");
        else System.out.println ("添加失败");
        return "redirect:/users";
    }
    @GetMapping("/user/{id}")
    public String toUpdateUser(@PathVariable("id") int id,Model model){
        User user = userMapper.queryUserById (id);
        model.addAttribute ("uuser",user);
        return "user/updateUser";
    }
    @PostMapping("/toupdateuser")
    public String updateUser(User user){
        int i = userMapper.updateUser (user);
        if(i>0) System.out.println ("添加成功");
        else System.out.println ("添加失败");
        return "redirect:/users";
    }
    @RequestMapping("/deleuser/{id}")
    public String deleteUser(@PathVariable("id") int id){
        int i = userMapper.deleteUser (id);
        if(i>0) System.out.println ("添加成功");
        else System.out.println ("添加失败");
        return "redirect:/users";
    }
}
